package com.git.hui.yinshua.api.model.banner;

import lombok.Getter;

import java.util.Locale;

/**
 * @author YiHui
 * @date 2024/6/17
 */
public enum BannerLinkEnum {
    ITEM("ITEM"),
    HTTP("HTTP"),
    APP("APP"),
    ;

    @Getter
    private String prefix;

    BannerLinkEnum(String prefix) {
        this.prefix = prefix;
    }

    public String buildUrl(String url) {
        String lowPrefix = prefix.toLowerCase();
        if (url.startsWith(lowPrefix)) {
            return url;
        }
        return lowPrefix + "://" + url;
    }

    public static BannerLinkEnum fromUrl(String url) {
        if (url == null) {
            return null;
        }
        if (url.startsWith(ITEM.prefix.toLowerCase(Locale.ROOT))) {
            return ITEM;
        } else if (url.startsWith(HTTP.prefix.toLowerCase(Locale.ROOT))) {
            return HTTP;
        } else if (url.startsWith(APP.prefix.toLowerCase(Locale.ROOT))) {
            return APP;
        } else {
            return null;
        }
    }
}
